
// NlcpfwDemoDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "NlcpfwDemo.h"
#include "NlcpfwDemoDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

int TranStrToHexStr(CString &cStr, const char *buf, int bufLen)//转成16进制
{
	cStr = _T("");
	CString tmpStr;

	for(int i=0; i<bufLen; i++)
	{
		tmpStr.Format(_T(" 0x%x"), buf[i]);
		cStr += tmpStr;
	}
	return cStr.GetLength();
}

void WINAPI NotifyRecvData(void *pBuf, UINT dwDataLen, void *pParam)
{
	if(pParam == NULL)
		return;
	CNlcpfwDemoDlg *pDlg = (CNlcpfwDemoDlg*)pParam;

	CString cstr1,cstr2,hexStr;
	TranStrToHexStr(hexStr, (char*)pBuf, dwDataLen);
	char buf[5000] = {0};
	memcpy(buf, pBuf, dwDataLen);

	if(dwDataLen == 369)	/*若接收到为版本信息*/
	{
		buf[dwDataLen-1] = 0;
		cstr2 = buf + 15;
	}
	else
	{
		buf[dwDataLen] = 0;
		cstr2 = buf;
	}

	cstr1.Format(_T("recv %d bytes"), dwDataLen);
	pDlg->m_edit_recv.SetWindowText(cstr1 + _T("\r\n") + cstr2 + _T("\r\n") + hexStr);
}

// CNlcpfwDemoDlg 对话框




CNlcpfwDemoDlg::CNlcpfwDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CNlcpfwDemoDlg::IDD, pParent)
{
	m_bEnableNotify = FALSE;
	m_hDev = FALSE;
}

void CNlcpfwDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_RECV, m_edit_recv);
}

BEGIN_MESSAGE_MAP(CNlcpfwDemoDlg, CDialog)
	//}}AFX_MSG_MAP
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BTN_UDP, &CNlcpfwDemoDlg::OnBnClickedBtnUdp)
	ON_BN_CLICKED(IDC_BTN_CLOSE, &CNlcpfwDemoDlg::OnBnClickedBtnClose)
	ON_BN_CLICKED(IDC_BTN_HID, &CNlcpfwDemoDlg::OnBnClickedBtnHid)
	ON_BN_CLICKED(IDC_BTN_COM, &CNlcpfwDemoDlg::OnBnClickedBtnCom)
	ON_BN_CLICKED(IDC_BTN_NOTIFY, &CNlcpfwDemoDlg::OnBnClickedBtnNotify)
	ON_BN_CLICKED(IDC_BTN_HEXSEND, &CNlcpfwDemoDlg::OnBnClickedBtnHexsend)
	ON_BN_CLICKED(IDC_BTN_SEND, &CNlcpfwDemoDlg::OnBnClickedBtnSend)
	ON_BN_CLICKED(IDC_BTN_CLEAR_EDIT, &CNlcpfwDemoDlg::OnBnClickedBtnClearEdit)
	ON_BN_CLICKED(IDC_BTN_CLEAR_DEV, &CNlcpfwDemoDlg::OnBnClickedBtnClearDev)
	ON_BN_CLICKED(IDC_BTN_READ, &CNlcpfwDemoDlg::OnBnClickedBtnRead)
END_MESSAGE_MAP()


// CNlcpfwDemoDlg 消息处理程序



BOOL CNlcpfwDemoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	GetDlgItem(IDC_EDIT_COM)->SetWindowText(_T("1"));
	GetDlgItem(IDC_EDIT_BAUD)->SetWindowText(_T("9600"));
	SetDlgItemText(IDC_BTN_NOTIFY, L"Enable callback");

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}



void CNlcpfwDemoDlg::OnClose()
{

	CDialog::OnClose();
}

int TCHAR2INT(TCHAR tChar)//16进制转成10进制
{
	int iRet;
	if(tChar >= _T('0') && tChar <= _T('9'))
		iRet = tChar - _T('0');
	else if(tChar >= _T('a') && tChar <= _T('f'))
		iRet = tChar - _T('a') + 10;
	if(tChar >= _T('A') && tChar <= _T('F'))
		iRet = tChar - _T('A') + 10;
	return iRet;
}

int HexStrToDataStr(char *pBuf, UINT iBufSize, CString hexStr)
{
	int nTemp[10];
	int iData;
	UINT iBufLen;
	ZeroMemory(nTemp, sizeof(nTemp));//置0
	iBufLen = hexStr.GetLength()/4;
	if(iBufSize <= iBufLen )
		return -1;
	for(UINT i=0; i<iBufLen; i++)// 16进制的字符串转成10进制数
	{
		nTemp[2] = TCHAR2INT(hexStr.GetAt(i*4+2));
		nTemp[3] = TCHAR2INT(hexStr.GetAt(i*4+3));
		iData = nTemp[2]*16 +nTemp[3];
		pBuf[i] = (char)iData;
	}
	return iBufLen;
}
void CNlcpfwDemoDlg::OnBnClickedBtnUdp()
{
	int iWorkMode = CPFW_OM_NORMAL;//正常模式，不支持回调函数
	if(((CButton*)GetDlgItem(IDC_CHECK_KEEPWOKE))->GetCheck() == 1)
		iWorkMode = CPFW_OM_KEEP;//连接模式，支持回调函数，支持断点重连
	m_hDev = cpfw_open(_T("udp"), NULL, iWorkMode);
	if(m_hDev == NULL)
	{
		MessageBox(_T("Open failed!"));
		return;
	}
	if(m_bEnableNotify && iWorkMode == CPFW_OM_KEEP)
		cpfw_register_notify(m_hDev, NULL, NotifyRecvData, this);
	MessageBox(_T("Open success!"));
	GetDlgItem(IDC_BTN_COM)->EnableWindow(FALSE);//变灰
	GetDlgItem(IDC_BTN_UDP)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_HID)->EnableWindow(FALSE);
	GetDlgItem(IDC_CHECK_KEEPWOKE)->EnableWindow(FALSE);
}

void CNlcpfwDemoDlg::OnBnClickedBtnClose()
{
	if(m_hDev)
		cpfw_close(m_hDev);
	m_hDev = NULL;
	GetDlgItem(IDC_BTN_COM)->EnableWindow(TRUE);
	GetDlgItem(IDC_BTN_UDP)->EnableWindow(TRUE);
	GetDlgItem(IDC_BTN_HID)->EnableWindow(TRUE);
	GetDlgItem(IDC_CHECK_KEEPWOKE)->EnableWindow(TRUE);
}

void CNlcpfwDemoDlg::OnBnClickedBtnHid()
{
	int iWorkMode = CPFW_OM_NORMAL;
	if(((CButton*)GetDlgItem(IDC_CHECK_KEEPWOKE))->GetCheck() == 1)
		iWorkMode = CPFW_OM_KEEP;
	m_hDev = cpfw_open(_T("hid"), NULL, iWorkMode);
	if(m_hDev == NULL)
	{
		MessageBox(_T("Open failed!"));
		return;
	}

	if(m_bEnableNotify && iWorkMode == CPFW_OM_KEEP)
		cpfw_register_notify(m_hDev, NULL, NotifyRecvData, this);
	MessageBox(_T("Open success!"));
	GetDlgItem(IDC_BTN_COM)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_UDP)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_HID)->EnableWindow(FALSE);
	GetDlgItem(IDC_CHECK_KEEPWOKE)->EnableWindow(FALSE);
}

void CNlcpfwDemoDlg::OnBnClickedBtnCom()
{
	int iPort = GetDlgItemInt(IDC_EDIT_COM);
	int iBps = GetDlgItemInt(IDC_EDIT_BAUD);
	WCHAR wStrParam[500] = {0};
	wsprintf(wStrParam, L"com=%d;baud=%d",iPort, iBps);

	int iWorkMode = CPFW_OM_NORMAL;
	if(((CButton*)GetDlgItem(IDC_CHECK_KEEPWOKE))->GetCheck() == 1)
		iWorkMode = CPFW_OM_KEEP;

	m_hDev = cpfw_open(_T("com"), wStrParam, iWorkMode);
	if(m_hDev == NULL)
	{
		MessageBox(_T("Open failed!"));
		return;
	}

	if(m_bEnableNotify && iWorkMode == CPFW_OM_KEEP)
		cpfw_register_notify(m_hDev, NULL, NotifyRecvData, this);
	MessageBox(_T("Open success!"));
	GetDlgItem(IDC_BTN_COM)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_UDP)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_HID)->EnableWindow(FALSE);
	GetDlgItem(IDC_CHECK_KEEPWOKE)->EnableWindow(FALSE);
}

void CNlcpfwDemoDlg::OnBnClickedBtnNotify()
{
	if(m_bEnableNotify)
	{
		if(m_hDev)
			cpfw_unregister_notify(m_hDev, TRUE, TRUE);
		m_bEnableNotify = FALSE;
		SetDlgItemText(IDC_BTN_NOTIFY, L"Enable callback");
	}
	else
	{
		if(m_hDev)
			cpfw_register_notify(m_hDev, NULL, NotifyRecvData, this);
		m_bEnableNotify = TRUE;
		SetDlgItemText(IDC_BTN_NOTIFY, L"Disable callback");
	}
}

void CNlcpfwDemoDlg::OnBnClickedBtnHexsend()
{
	CString hexStr;
	GetDlgItem(IDC_EDIT_SEND)->GetWindowText(hexStr);
	char buf[50] = {0};
	int buflen = HexStrToDataStr(buf, 50, hexStr);
	int ret = cpfw_write(m_hDev,buf, buflen, 200);
	if(ret > 0)
		MessageBox(L"Sent successfully!");
}

void CNlcpfwDemoDlg::OnBnClickedBtnSend()
{
	CString cstr;
	GetDlgItem(IDC_EDIT_SEND)->GetWindowText(cstr);
	USES_CONVERSION;
	int ret = cpfw_write(m_hDev,T2A(cstr), cstr.GetLength(), 200);
	if(ret > 0)
		MessageBox(L"Sent successfully!");
}

void CNlcpfwDemoDlg::OnBnClickedBtnClearEdit()
{
	m_edit_recv.SetWindowText(_T(""));
}

void CNlcpfwDemoDlg::OnBnClickedBtnClearDev()
{
	cpfw_clear(m_hDev);
}

void CNlcpfwDemoDlg::OnBnClickedBtnRead()
{
	while(1)
	{
		char buf[6000] = {0};
		int iReadCnt = cpfw_read(m_hDev, buf, 5999, 500, TRUE);
		if(iReadCnt < 0)
		{
			//MessageBox(L"read failed");
			//return;
		}
		else if(iReadCnt == 0)
		{
			//MessageBox(L"Read timeout!");
			//return;
		}
		else if(iReadCnt > 0)
		{
			CString cstr1,cstr2,hexStr;
			TranStrToHexStr(hexStr, buf, iReadCnt);//转成16进制数
			cstr2 = buf;
			cstr1.Format(_T("recv %d bytes"), iReadCnt);
			m_edit_recv.SetWindowText(cstr1 + _T("\r\n") + cstr2 + _T("\r\n") + hexStr);
			//MessageBox(cstr1 + _T("\r\n") + cstr2 + _T("\r\n") + hexStr);
			
		}
		Sleep(50);
	}
	//char buf[6000] = {0};

	//int iReadCnt = cpfw_read(m_hDev, buf, 5999, 500, TRUE);
	//if(iReadCnt < 0)
	//{
	//	MessageBox(L"read failed");
	//	return;
	//}
	//else if(iReadCnt == 0)
	//{
	//	MessageBox(L"Read timeout!");
	//	return;
	//}

	//CString cstr1,cstr2,hexStr;
	//TranStrToHexStr(hexStr, buf, iReadCnt);//转成16进制数
	//cstr2 = buf;
	//cstr1.Format(_T("recv %d bytes"), iReadCnt);
	//m_edit_recv.SetWindowText(cstr1 + _T("\r\n") + cstr2 + _T("\r\n") + hexStr);
}
